// pages/bidbond/bidbond.js
import data from './data';

const app = getApp();
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        items: [{
                name: 'USA',
                value: '建设银行'
            },
            {
                name: 'CHN',
                value: '中国银行',
                checked: 'true'
            },
        ],
        options1: data,
        value1: [],
        startdate: "选择",
        enddate: "选择",
        letter_company: "选择",
        ischecked: false,
		image_all:[],
		isweb:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            id: options.id
        })
        this.getLetterDetail();

    },
    //获取保函详情
    getLetterDetail: function(e) {
        var that = this;
        utils.http("/api/v1/common/get_letter_detail", {
            letter_id: that.data.id
        }, function(res) {
            that.setData({
                detail: res.datas,
                bank_arr: res.datas.bank_arr, //出具银行
                company_arr: res.datas.company_arr, //企业选择
            })
        })
    },
    onOpen1() {
        this.setData({
            visible1: true
        })
    },
    onClose1() {
        this.setData({
            visible1: false
        })
    },
    onChange1(e) {
        this.setData({
            title1: e.detail.options.map((n) => n.label).join('/')
        })
        //console.log(this.data.title1);
        //  console.log('onChange1', e.detail)
    },

    uploadAction: function(e) {
        var that = this;
		wx.navigateTo({
			url: '../webviews/webviews',
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
        // wx.chooseMessageFile({
        //     count: 10,
        //     type: 'file',
        //     success(res) {
        //         // tempFilePath可以作为img标签的src属性显示图片
        //         const tempFiles = res.tempFiles
        //         utils.showLoading("文件上传中");
        //         wx.uploadFile({
        //             url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
        //             header: {
        //                 'Content-Type': 'multipart/form-data'
        //             },
        //             filePath: tempFiles[0].path,
        //             name: 'file',
        //             formData: {
        //                 key: app.globalData.key
        //             },
        //             success(result) {
        //                 utils.hideLoading();
        //                 var res = JSON.parse(result.data);
        //                 that.setData({
        //                     filePath: res.path,
        //                     tempFiles: tempFiles[0].name
        //                 })
        //             },
        //             fail: function(res) {
        //                 utils.hideLoading();
        //                 utils.message('上传服务器上传失败,请稍后再试2');
        //                 console.log(res);
        //             }
        //         })
        //     }
        // })
    },
    //开标日期
    startDateChange: function(e) {
        this.setData({
            startdate: e.detail.value
        })
    },
    //截止日期
    endDateChange: function(e) {
        this.setData({
            enddate: e.detail.value
        })
    },
    //出具银行
    radioChange: function(e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value)
        this.setData({
            letter_bank: e.detail.value
        })
    },
    //企业选择
    bindPickerChange: function(e) {
        var that = this;
        this.setData({
            letter_company: that.data.company_arr[e.detail.value]
        })
    },
    //申请
    checkboxChange: function(e) {
        var that = this;
        if (that.data.ischecked == true) {
            that.setData({
                ischecked: false
            })
        } else {
            that.setData({
                ischecked: true
            })
        }

        //console.log(that.data.ischecked)
    },
    //跳转确认订单
    formSubmit: function(e) {
        var that = this;
		
        var letter_price = e.detail.value.letter_price; //投保金额
        var date = that.data.startdate + "," + that.data.enddate; //投保保护期限
        var letter_bank = that.data.letter_bank; //出具银行
        var letter_company = that.data.letter_company; //企业选择
        var letter_address = that.data.title1; //项目地址
		var letter_files = app.globalData.docurl;
        var ischecked = that.data.ischecked; //《投标保函承诺书》
	
        if (letter_price==""){
        	wx.showToast({
        		title: '请输入投保金额',
        		icon:"none"
        	})
        	return false;
        }
        if (letter_price>80) {
        	wx.showToast({
        		title: '担保金额<80万',
        		icon: "none"
        	})
        	return false;
        }
        if (that.data.startdate == "选择") {
            wx.showToast({
                title: '请选择开标日期',
                icon: "none"
            })
            return false;
        }
        if (that.data.enddate == "选择") {
        	wx.showToast({
        		title: '请选择截止日期',
        		icon: "none"
        	})
        	return false;
        }
        if (letter_bank == undefined) {
            wx.showToast({
                title: '请选择出具银行',
                icon: "none"
            })
            return false;
        }
        if (letter_company=="选择"){
            wx.showToast({
                title: '请选择企业',
                icon: "none"
            })
            return false;
        }
        if (letter_address == undefined) {
            wx.showToast({
                title: '请选择项目地址',
                icon: "none"
            })
            return false;
        }

		if (that.data.image_all.length<=0) {
        	wx.showToast({
        		title: '请上传照片',
        		icon: "none"
        	})
        	return false;
        }
        if (letter_files == undefined) {
        	wx.showToast({
        		title: '请上传文件',
        		icon: "none"
        	})
        	return false;
        }
        if (that.data.ischecked == false) {
            wx.showToast({
				title: '请勾选《投标保函承诺书》',
                icon: "none"
            })
            return false;
        }
		var letter_files_images=[];
		for (var i in that.data.image_all) {
			letter_files_images.push(that.data.image_all[i].id)
		}
        wx.setStorageSync("detail", {
			letter_id: that.data.id,
			letter_price: letter_price,
			letter_date: date,
			letter_bank: letter_bank,
			letter_company: letter_company,
			letter_address: letter_address,
			letter_files_images: letter_files_images,
			letter_files: app.globalData.docurl,
			letter_jiafang:"",
			letter_project:""
		})
		wx.navigateTo({
			url: "../butorder/butorder",
		})

    },
    //《投标保函承诺书》
    onLetterTap: function(e) {
        wx.navigateTo({
            url: "../letter/letter",
        })
    },
    uploadImage: function() {
        var that = this;
        wx.chooseImage({
            success(res) {
                utils.showLoading("图片上传中");
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
                    header: {
                        'Content-Type': 'multipart/form-data'
                    },
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: app.globalData.key
                    },
                    success: function(result) {
                        utils.hideLoading();
                        var image_all = that.data.image_all || [];
                        //上传图片后返回的是字符串，需json.prise一下
                        var res = JSON.parse(result.data);
                        image_all.push({
							image:res.path,
							id:res.id
						})
                        that.setData({
                            image_all: image_all
                        })
                        console.log(that.data.image_all)
                    },
                    fail: function(res) {
                        utils.hideLoading();
                        utils.message('上传服务器上传失败,请稍后再试2');
                        console.log(res);
                    }
                })
            }
        })
    },

	//删除照片
	bindCloseTap:function(e){
	  var that=this;
		for (var i in that.data.image_all){
			if (that.data.image_all[i].id == e.currentTarget.dataset.item){
				that.data.image_all.splice(i,1);
			}
		}
		
		that.setData({
			image_all: that.data.image_all
		})
		console.log(that.data.image_all)
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})